package com.xiaoyu.client.adapter.discovery;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.QuickContactBadge;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.discovery.DiscoveryRankingBean;
import com.xiaoyu.client.model.discovery.RankParam;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.activity.help.RankingActivity;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.utils.CCircleImageView;
import com.xiaoyu.commonlib.utils.ToastUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jk on 2018/10/22.
 */
public class DiscoveryRankAdapter extends BaseAdapter {
    public static final String RANK_TYPE = "ranking_type";
    private Context mContext;
    private Map<Integer, List<RankParam>> mList = new HashMap<>();

    public DiscoveryRankAdapter(Context mContext, Map<Integer, List<RankParam>> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public List<RankParam> getItem(int position) {
        if (mList.containsKey(position))
            return mList.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder {
        TextView rankTitle;
        TextView moreBtn;
        LinearLayout rankLayout;
        CCircleImageView onePhoto;
        CCircleImageView twoPhoto;
        CCircleImageView threePhoto;
        TextView oneName;
        TextView twoName;
        TextView threeName;
        TextView oneAttenBtn;
        TextView twoAttenBtn;
        TextView threeAttenBtn;
        ImageView mBlankImg;//空页面

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_discovery_ranking, null);
            holder = new ViewHolder();
            holder.rankTitle = convertView.findViewById(R.id.item_discovery_ranking_title_txt);
            holder.moreBtn = convertView.findViewById(R.id.item_discovery_ranking_more_btn);
            holder.rankLayout = convertView.findViewById(R.id.item_discovery_ranking_user_layout);
            holder.onePhoto = convertView.findViewById(R.id.item_discovery_ranking_one_photo);
            holder.oneName = convertView.findViewById(R.id.item_discovery_ranking_one_name_txt);
            holder.oneAttenBtn = convertView.findViewById(R.id.item_discovery_ranking_one_attention_btn);
            holder.twoPhoto = convertView.findViewById(R.id.item_discovery_ranking_two_photo);
            holder.twoName = convertView.findViewById(R.id.item_discovery_ranking_two_name_txt);
            holder.twoAttenBtn = convertView.findViewById(R.id.item_discovery_ranking_two_attention_btn);
            holder.threePhoto = convertView.findViewById(R.id.item_discovery_ranking_three_photo);
            holder.threeName = convertView.findViewById(R.id.item_discovery_ranking_three_name_txt);
            holder.threeAttenBtn = convertView.findViewById(R.id.item_discovery_ranking_three_attention_btn);
            holder.mBlankImg = convertView.findViewById(R.id.item_discovery_ranking_user_layout_img);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final List<RankParam> list = getItem(position);
        if (position == 0) {//日榜
            holder.rankTitle.setText("互动日榜");
        }
        if (position == 1) {
            holder.rankTitle.setText("互动周榜");
        }
        if (position == 2) {
            holder.rankTitle.setText("互动月榜");
        }

        if (list.size() == 0) {
            holder.rankLayout.setVisibility(View.GONE);
            holder.mBlankImg.setVisibility(View.VISIBLE);

        } else {
            holder.rankLayout.setVisibility(View.VISIBLE);
            holder.mBlankImg.setVisibility(View.GONE);
            if (list.size() == 1) {
                Glide.with(mContext).load(list.get(0).getUserPhoto()).into(holder.onePhoto);
                holder.oneName.setText(list.get(0).getNickname());
                holder.rankLayout.getChildAt(1).setVisibility(View.INVISIBLE);
                holder.rankLayout.getChildAt(2).setVisibility(View.INVISIBLE);
                if (list.get(0).isAttention()){//已关注
                    holder.oneAttenBtn.setText("已关注");
                }else {
                    holder.oneAttenBtn.setText("关注");
                }
            }

            if (list.size() == 2) {
                Glide.with(mContext).load(list.get(0).getUserPhoto()).into(holder.onePhoto);
                holder.oneName.setText(list.get(0).getNickname());
                Glide.with(mContext).load(list.get(1).getUserPhoto()).into(holder.twoPhoto);
                holder.twoName.setText(list.get(1).getNickname());
                holder.rankLayout.getChildAt(2).setVisibility(View.INVISIBLE);

                if (list.get(0).isAttention()){//已关注
                    holder.oneAttenBtn.setText("已关注");
                }else {
                    holder.oneAttenBtn.setText("关注");
                }

                if (list.get(1).isAttention()){//已关注
                    holder.twoAttenBtn.setText("已关注");
                }else {
                    holder.twoAttenBtn.setText("关注");
                }
            }

            if (list.size() == 3) {
                Glide.with(mContext).load(list.get(0).getUserPhoto()).into(holder.onePhoto);
                holder.oneName.setText(list.get(0).getNickname());
                Glide.with(mContext).load(list.get(1).getUserPhoto()).into(holder.twoPhoto);
                holder.twoName.setText(list.get(1).getNickname());
                Glide.with(mContext).load(list.get(2).getUserPhoto()).into(holder.threePhoto);
                holder.threeName.setText(list.get(2).getNickname());

                if (list.get(0).isAttention()){//已关注
                    holder.oneAttenBtn.setText("已关注");
                }else {
                    holder.oneAttenBtn.setText("关注");
                }

                if (list.get(1).isAttention()){//已关注
                    holder.twoAttenBtn.setText("已关注");
                }else {
                    holder.twoAttenBtn.setText("关注");
                }

                if (list.get(2).isAttention()){//已关注
                    holder.threeAttenBtn.setText("已关注");
                }else {
                    holder.threeAttenBtn.setText("关注");
                }

            }

        }

        //查看更多按钮
        holder.moreBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, RankingActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt(RANK_TYPE, position);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });

        //第一名关注按钮
        holder.oneAttenBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toAttention(holder.oneAttenBtn,position,0);
            }
        });

        //第二名关注按钮
        holder.twoAttenBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toAttention(holder.twoAttenBtn,position,1);
            }
        });

        //第三名关注按钮
        holder.threeAttenBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toAttention(holder.threeAttenBtn,position,2);
            }
        });


        return convertView;
    }


    private void toAttention(final TextView view, final int itemId, final int rankId){
        NetworkManager.sendAttentionUser(getItem(itemId).get(rankId).getUserid(), new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                Log.i("zhangmin","guanzhu result= "+result);
                if (getItem(itemId).get(rankId).isAttention()){//已关注
                    getItem(itemId).get(rankId).setFocuson("false");
                    view.setText("关注");
                }else {
                    view.setText("已关注");
                    getItem(itemId).get(rankId).setFocuson("true");
                }

            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
                ToastUtil.showToast(mContext,"关注失败");
                Log.i("zhangmin","guanzhu errorCode = "+errorCode+"  errorDiscription = "+errorDiscription);
            }
        });
    }
}
